package com.xunyin.enums;

/**
 * @author jthong
 * @Description:指令前缀
 * @Version 1.0.0
 * @Date 2019/12/26 16:18
 */
public enum CommandPrefixEnum {
    /***
     * 心跳协议前缀
     */
    HEARTBEAT(1, "AA88BAAB66BB"),

    /***
     * 包间门禁二维码指令前缀
     */
    ROOM_QR_CODE(2, "B6ABA8"),

    /***
     * 大门门禁二维码指令前缀
     */
    GATE_QR_CODE(3, "A8BAB6"),

    /***
     * 大门门禁二维码指令前缀
     */
    MANAGER_GATE_QR_CODE(4, "B8ABA6");

    private final String name;
    private final int value;

    CommandPrefixEnum(int value, String name) {
        this.name = name;
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return value;
    }

    public static CommandPrefixEnum parse(Integer value) {
        if (value == null) {
            return throwParseException(value);
        }
        for (CommandPrefixEnum e : CommandPrefixEnum.values()) {
            if (e.getValue() == value) {
                return e;
            }
        }
        return throwParseException(value);
    }

    private static CommandPrefixEnum throwParseException(Integer value) {
        throw new IllegalArgumentException(value + " can not parse to " + CommandPrefixEnum.class.getName());
    }

    public static String getNameByValue(Integer value) {
        if (value == null) {
            return "";
        }
        for (CommandPrefixEnum e : CommandPrefixEnum.values()) {
            if (e.getValue() == value) {
                return e.getName();
            }
        }
        return "";
    }
}
